//
//  UserRegisterVC.m
//  danpin
//
//  Created by lcb on 6/29/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UserRegisterVC.h"
#import "RTLabel.h"
#import "BaseWebViewController.h"
#import "ZToastManager.h"
#import "BaseModel.h"
#import "BaseCell.h"
@interface UserRegisterVC ()<UITextFieldDelegate>
@property (weak) id<UserDelegate> delegate;
@property(nonatomic,strong) UITextField     *txtUser;
@property(nonatomic,strong) UITextField     *txtName;
@property(nonatomic,strong) UITextField     *txtPass;
@property(nonatomic,strong) RTLabel *serviceLabel;
@property(nonatomic,strong) UIImageView  *iconView;
@property(nonatomic,strong) UILabel  *logLabel;
+ (instancetype)newController;
@end

@implementation UserRegisterVC
+ (instancetype)newController
{
    return [super new];
}
+ (instancetype)newWithDelegate:(id<UserDelegate>)delegate
{
    UserRegisterVC *viewController = [UserRegisterVC newController];
    
    if (viewController) {
        viewController.delegate = delegate;
    }
    
    return viewController;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor whiteColor];
    self.rightTitle =@"完成";
    self.navTitle =@"会员注册";
    [UMengShare event:CK_USERRIGESTER acc:1];
    [self initUI];
    UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
    self.view.userInteractionEnabled =YES;
    [self.view addGestureRecognizer:singleRecognizer];
    singleRecognizer=nil;
}

-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            [self okAction];
        }
            break;
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(UIImageView*)iconView
{
    if (!_iconView) {
        self.iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
        self.iconView.frame = CGRectMake((DEVICE_WIDTH-80)/2,35, 80, 80);
        self.iconView.layer.cornerRadius=40;
        self.iconView.layer.masksToBounds = YES;
        self.iconView.image =[UIImage imageNamed:@"user_face.png"];
        self.iconView.userInteractionEnabled =YES;
        [self.view addSubview:self.iconView];
    }
    
    return _iconView;
}
-(UILabel*)logLabel
{
    if (!_logLabel) {
        self.logLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        self.logLabel.backgroundColor =[UIColor clearColor];
        self.logLabel.font = FONT(12);
        self.logLabel.textColor =UIColorFromRGB(0xcccccc);
        self.logLabel.textAlignment = NSTextAlignmentCenter;
        self.logLabel.text =@"单品 - 以一顶百";
        [self.view addSubview:self.logLabel];
    }
    return _logLabel;
}

- (RTLabel *)serviceLabel {
    if (!_serviceLabel) {
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom1)];
        self.serviceLabel = [[RTLabel alloc] initWithFrame:CGRectZero];
        self.serviceLabel.backgroundColor =[UIColor clearColor];
        self.serviceLabel.textColor =UIColorFromRGB(0xcccccc);
        self.serviceLabel.font =FONT(13);
        self.serviceLabel.textAlignment =NSTextAlignmentCenter;
        self.serviceLabel.userInteractionEnabled =YES;
        [self.serviceLabel addGestureRecognizer:singleRecognizer];
        [self.view addSubview:self.serviceLabel];
        singleRecognizer=nil;
    }
    return _serviceLabel;
}

-(UITextField *)txtUser
{
    if (!_txtUser) {
        self.txtUser = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtUser.placeholder = @" 邮箱地址";
        [self.txtUser setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtUser.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUser.font = FONT(13);
        self.txtUser.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtUser.returnKeyType = UIReturnKeyNext;
        self.txtUser.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUser.delegate=self;
        [self.view addSubview:self.txtUser];
    }
    return _txtUser;
}
-(UITextField *)txtName
{
    if (!_txtName) {
        self.txtName = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtName.placeholder = @" 昵称";
        [self.txtName setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtName.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtName.font = FONT(13);
        self.txtName.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtName.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtName.returnKeyType = UIReturnKeyNext;
        self.txtName.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtName.delegate=self;
        [self.view addSubview:self.txtName];
    }
    return _txtName;
}

-(UITextField *)txtPass
{
    if (!_txtPass) {
        self.txtPass = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtPass.placeholder = @" 密码";
        [self.txtPass setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtPass.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPass.font = FONT(13);
        self.txtPass.secureTextEntry = YES;
        self.txtPass.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtPass.returnKeyType = UIReturnKeyGo;
        self.txtPass.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtPass.delegate=self;
        [self.view addSubview:self.txtPass];
    }
    return _txtPass;
}
-(void)handleSingleTapFrom
{
    [self.txtUser resignFirstResponder];
    [self.txtName resignFirstResponder];
    [self.txtPass resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField==self.txtUser) {
        [self.txtName becomeFirstResponder];
    }else if (textField==self.txtName){
        [self.txtPass becomeFirstResponder];
    }else if (textField==self.txtPass){
        [self btnLoginAction];
        [textField resignFirstResponder];
    }else
    {
        [textField resignFirstResponder];
    }
    return YES;
}
-(void)initUI
{

    self.txtUser.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, 100, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    self.txtName.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, 150, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    self.txtPass.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, 200, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);

      NSString *text=@"注册表示你同意 <font size=13 color='#285790'>服务条款 </font> 和 <font size=13 color='#285790'>隐私政策 </font>";
     [self.serviceLabel setText:text];
     CGSize optimumSize1 = [self.serviceLabel optimumSize];
     self.serviceLabel.frame=CGRectMake((DEVICE_WIDTH-optimumSize1.width)/2, 250, optimumSize1.width, 20);
    
    
}
-(void)handleSingleTapFrom1
{
    BaseWebViewController *web =[[BaseWebViewController alloc] init];
    web.titleStr =@"服务协议";
    web.urlStr =@"http://h5.m.danpin.com/help/user.html";
    [self.navigationController pushViewController:web animated:YES];
}
-(void)okAction
{
    [self btnLoginAction];
}
- (void)btnLoginAction
{
    if(self.txtUser.text.length==0)
    {
        [[ZToastManager ShardInstance] showtoast:@"请输入邮箱地址"];
        return;
    }
    if(self.txtName.text.length==0)
    {
        [[ZToastManager ShardInstance] showtoast:@"请输入昵称"];
        return;
    }
    if(self.txtPass.text.length==0)
    {
        [[ZToastManager ShardInstance] showtoast:@"请输入密码"];
        return;
    }
    [[ZToastManager ShardInstance] showprogress];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    
    [dict setValue:@"register" forKey:@"action"];
    [dict setValue:self.txtName.text forKey:@"nickname"];
    [dict setValue:self.txtUser.text forKey:@"email"];
    [dict setValue:self.txtPass.text forKey:@"password"];
    [BaseModel user:dict success:^(AFHTTPRequestOperation* operation, NSObject* result){
        [[ZToastManager ShardInstance] hideprogress];
        [wSelf parserResultObject:result];
    } failure:^(NSError* error){
        [[ZToastManager ShardInstance] hideprogress];
        [[ZToastManager ShardInstance] showtoast:@"请求失败"];
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        {
            [[ZToastManager ShardInstance] showtoast:[resultObject objectForKey:@"msg"]];
            if ([[resultObject objectForKey:@"code"] intValue]==1)
            {
                [[ZShare sharedInstance] initUserInfo:resultObject];
                if (_delegate&&[_delegate respondsToSelector:@selector(userLoginOK:)]) {
                    [_delegate userLoginOK:[ZShare sharedInstance].userInfo];
                }
            }
        }
    }
}
@end
